<?
require_once '../system/init.php';
$album = new Albums();
$album->addAlbums();

$folders = array_keys($album->getFoldersFromStorage());
$albumsFolders = $album->getAlbumsFoldersFromDb();


$folders = array_diff($folders, $albumsFolders);
require_once 'inc/header.php';
// Страница только для залогиненных пользователей
Common::checkLogin($user);
?>

<div class="main">
    <div class="center">
        <ul class="menu">
            <li><a href="/index.php" class="btn btn-active">Альбомы</a></li>
            <li><a href="/tv/all_events.php" class="btn">События</a></li>
        </ul>

        <div id="user-info" class="h">
            <span id="user-name"></span>
            <span id="user-lastname"></span>
        </div>
    </div>
    <div class="clear"></div>
    <fieldset><legend><h1>Добавление альбома</h1></legend></fieldset>

    <div id="add-form"style="width: 500px; margin: 0 auto;">
        <form method="post" enctype="multipart/form-data" onsubmit="return checkForm ( );">
            <table class="form" cellpadding="0" cellspacing="0">
                <tr>
                    <td class="field-name"><label for="album_name">Название</label>
                    <td><input type="text" name="album_name" id="album_name" class="w-400" onFocus="hideError(this.id);" /></td>
                    <td class="errors" id="album_name-error"></td>
                </tr>
                <tr>
                    <td class="field-name"><label for="album_author">Автор</label></td>
                    <td><input type="text" name="album_author" id="album_author" class="w-400" value="" /></td>
                    <td class="errors"></td>
                </tr>
                <tr>
                    <td class="field-name">Фотографии</td>
                    <td class="middle">
                        <input type="file" value="" name="photos[]" onClick="hideError(this.id)" id="files-upload" multiple>

                        <? if (!empty($folders)): ?>
                        <span class="relative folders-albums">или папка с ними&nbsp;&nbsp;</span>
                        <select name="album_folder" id="album_folder" onClick="hideError(this.id)">
                            <option value="" selected>--</option>
                            <? foreach ($folders as $folder): ?>
                            <option value="<?=$folder?>"><?=$folder?></option>
                            <? endforeach ?>
                        </select>
                        <? endif ?>
                    </td>
                    <td class="errors" id="files-upload-error"></td>
                </tr>
                <tr>
                    <td class="field-name"></td>
                    <td><input type="submit" name="submit" class="btn photo-info-save" /></td>
                </tr>
            </table>
        </form>
    </div>
</div>

<script>

    function checkForm() {
        var name    = $("#album_name"),
            photos = $("input[type=file]");
        if (name.val() && (photos.val() || $("#album_folder").val())) {
            return true;
        } else {
            if (!name.val()) {
                name.css('border','1px #ff6600 solid');
                $("#album_name-error").text('введите название альбома');
            }

            if (!photos.val() && !$("#album_folder").val()) {
                $("#files-upload-error").text('нет выбранных файлов');
            }

            return false;
        }
    }

    function hideError(inputFieldId) {

        $("#"+inputFieldId+"-error").text('');
        if (inputFieldId != 'files-upload') {
            $("#"+inputFieldId).css('border','1px #ccc solid');
        }

    }
</script>


<script>
    $(function(){
        $.datepicker.setDefaults(
            $.extend($.datepicker.regional["ru"])
        );
        $(".datepicker").datepicker({
            dateFormat:'yy-mm-dd'
        });
    });

</script>


<? require_once 'inc/footer.php'; ?>